package al.softsynergy.auk.ui.ankesa;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import al.softsynergy.auk.data.containers.AnkesaContainer;
import al.softsynergy.auk.data.entries.Ankesa;

import com.vaadin.data.Item;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.DateField;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Field;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Select;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;

public class AnkesaForm extends Form implements ClickListener{

	
	private Button save = new Button("Ruaj", (ClickListener) this);
	//private Button cancel = new Button("Anullo", (ClickListener) this);
	private Button edit = new Button("Ndrysho", (ClickListener) this);
	
	private AnkesaUI ankesaUI;
	private boolean addAnkesaMode = false;
	private Ankesa newAnkese  = null;
	
	private final Select ankesaLlojSelect;
	
	
	
	public AnkesaForm(AnkesaUI ankesaUI){
		setSizeFull();
		this.ankesaUI = ankesaUI;
		this.ankesaLlojSelect = new Select("Ankesa Lloj",this.ankesaUI.getAnkesaLlojContainer());
		
		setWriteThrough(false);
		this.setInvalidCommitted(false);
		HorizontalLayout footer = new HorizontalLayout();
		footer.setSpacing(true);
		footer.addComponent(save);
		//footer.addComponent(cancel);
		footer.addComponent(edit);
		footer.setVisible(false);

		setFooter(footer);
		
		setFormFieldFactory(new DefaultFieldFactory(){
			@Override
			public Field createField(Item item, Object propertyId,
					Component uiContext) {
				Field field = super.createField(item, propertyId, uiContext);
				
				if(propertyId.equals("emri")){
					TextField tf = (TextField) field;
					 tf.setMaxLength(30);
		             tf.setRequired(true);
		             tf.setRequiredError("Emri nuk duhet te jete bosh!");
		             tf.setWidth("200px");
		             tf.setNullRepresentation("");
		             return tf;
				}
				
				if(propertyId.equals("mbiemri")){
					TextField tf = (TextField) field;
					 tf.setMaxLength(30);
		             tf.setRequired(true);
		             tf.setRequiredError("Mbiemri nuk duhet te jete bosh!");
		             tf.setWidth("200px");
		             tf.setNullRepresentation("");
		             return tf;
				}
				
				if(propertyId.equals("adresa")){
					TextField tf = (TextField) field;
					 tf.setMaxLength(100);
		             tf.setRequired(true);
		             tf.setRequiredError("Adresa nuk duhet te jete bosh!");
		             tf.setWidth("200px");
		             tf.setNullRepresentation("");
		             return tf;
				}
				
				if(propertyId.equals("tel")){
					TextField tf = (TextField) field;
					 tf.setMaxLength(20);
		             tf.setWidth("200px");
		             tf.setNullRepresentation("");
		             return tf;
				}
				
				if (propertyId.equals("ankesaLloj")){
					ankesaLlojSelect.setRequired(true);
					ankesaLlojSelect.setRequiredError("Lloji i ankeses nuk duhet te jete bosh!");
					ankesaLlojSelect.setItemCaptionMode(Select.ITEM_CAPTION_MODE_PROPERTY);
					ankesaLlojSelect.setItemCaptionPropertyId("tag");
					ankesaLlojSelect.setWidth("200px");
					return ankesaLlojSelect;
				}
				
				if(propertyId.equals("dataAnkese")){
					DateField dt = (DateField)field;
					dt.setResolution(DateField.RESOLUTION_DAY);
					dt.setRequired(true);
					dt.setRequiredError("Data e ankeses nuk duhet te jete bosh !");
					dt.setWidth("200px");
		            dt.setValue(new Date());
		            return dt;
				}
				
				if(propertyId.equals("pershkrimi")){
					TextField tf = (TextField) field;
		            tf.setNullRepresentation("");
		            tf.setRows(5); // this will make it an 'area', i.e multiline
		            tf.setColumns(40);
		            tf.setImmediate(true);
		            return tf;
				}
				
				return null;
			}
			
			});
		
	}
	
	@Override
	public void setItemDataSource(Item newDataSource) {

		this.addAnkesaMode = false;
		this.setVisible(true);
		if(newDataSource != null){
			setReadOnly(false);
			List<Object> orderedProperties = Arrays.asList(AnkesaContainer.NATURAL_COL_ORDER_FOR_FORM);
			super.setItemDataSource(newDataSource, orderedProperties);
			setReadOnly(true);
			getFooter().setVisible(true);
			
		}
		else{
			super.setItemDataSource(null);
			this.setVisible(false);
			getFooter().setVisible(false);
		}
	}
	
	@Override
	public void setReadOnly(boolean readOnly) {
		super.setReadOnly(readOnly);
		save.setVisible(!readOnly);
		//cancel.setVisible(!readOnly);
		edit.setVisible(readOnly);

	}
	
	public void addAnkese(){
		newAnkese = new Ankesa();
		newAnkese.setDataAnkese(new Date());
		setItemDataSource(new BeanItem(newAnkese));
		addAnkesaMode = true;
		setReadOnly(false);
	}
	
	@Override
	public void buttonClick(ClickEvent event) {
		Button source = event.getButton();
		if (source == this.save){
			try {
				this.commit();
				newAnkese = this.ankesaUI.getAukBean().saveAnkesa(((BeanItem<Ankesa>)this.getItemDataSource()).getBean());
				if (addAnkesaMode){
					Item addedItem = ankesaUI.getDataSource().addItem(newAnkese);
					setItemDataSource(addedItem);
					addAnkesaMode = false;
				}
				setReadOnly(true);
				//cancel.setVisible(true);
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		else if (source == edit) {
			setReadOnly(false);
		}
		
	}

}
